package com.icard.wx.icardwx.service.impl;

import com.dsbj.common.msg.ResponseMessage;
import com.icard.wx.icardwx.model.AllowUserResponseMessage;
import com.icard.wx.icardwx.model.WxUser;
import com.icard.wx.icardwx.model.WxUserResponseMessage;
import com.icard.wx.icardwx.service.IUserService;
import com.icard.wx.icardwx.utils.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class UserService implements IUserService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public ResponseMessage getWxUserByOpenId(String openId) {

        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append(String.format("/api/icard/user/getUserByOpenId?openId=%s",openId));
        WxUserResponseMessage msg =  restTemplate.getForObject(sb.toString(), WxUserResponseMessage.class);
        if(!msg.isSuccess()||msg.getWxUser()==null){
            return ResponseMessage.error(msg.getMessage());
        }
        return ResponseMessage.ok(msg.getWxUser());
    }

    @Override
    public ResponseMessage bindWxUser(Integer type,String openId, String username, String password,String employeeNo,String phoneNo,String vCode) {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append(String.format("/api/icard/user/wxBindUser?type=%d&openId=%s&username=%s&password=%s&employeeNo=%s&phoneNo=%s&vCode=%s",type,openId,username,password,employeeNo, phoneNo, vCode));
        ResponseMessage msg =  restTemplate.getForObject(sb.toString(), ResponseMessage.class);
        return msg;
    }
}
